package org.antlr.runtime.misc;

import java.util.Vector;

public class Util {

	public static String replaceAll(String str, String pattern, String replace) {
		int s = 0;
		int e = 0;
		StringBuffer result = new StringBuffer();

		while ((e = str.indexOf(pattern, s)) >= 0) {
			result.append(str.substring(s, e));
			result.append(replace);
			s = e + pattern.length();
		}
		result.append(str.substring(s));
		return result.toString();
	}

	public static String roundToString(double d, int decimalPositions) {
		StringBuffer buf = new StringBuffer();
		int n = (int) d;
		buf.append(n);
		d = Math.abs(d) - Math.abs(n);
		if (decimalPositions > 0) {
			buf.append('.');
			for (int i = 0; i < decimalPositions - 1; i++) {
				d *= 10;
				n = (int) d;
				buf.append(n);
				d -= n;
			}
			d = d * 10 + 0.5;
			n = (int) d;
			buf.append(n);
		}
		return buf.toString();
	}

	public static Vector subList(Vector v, int fromIndex, int toIndex) {
		if (fromIndex < 0 || toIndex > v.size() || fromIndex > toIndex)
			throw new IndexOutOfBoundsException();
		int len = toIndex - fromIndex;
		Vector vv = new Vector(len);
		for (int i = 0; i < len; i++)
			vv.addElement(v.elementAt(fromIndex + i));
		return vv;
	}
}
